package kyuu.acshmily
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	
	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.events.ResizeEvent;

	// 绑定事件：complete，进度条走完时调度
	[Event(name="complete", type="flash.events.Event")]
	// 绑定事件：progress，进度条进行中调度
	[Event(name="progress", type="flash.events.ProgressEvent")]

	public class KyuuProgressBar extends UIComponent
	{
		// 大葱图片
		[Embed(source="../../../res/negi.png")]
		[Bindable]
		private var NegiImage:Class;

		// 位图资源
		private var negiBmp:Bitmap = new NegiImage() as Bitmap;
		// 进度条组件
		private var barImg:Image = null;

		// 其他属性
		private var _proggressMax:Number = -1;	// 最大宽度 
		private var _sliderMax:Number = -1;		// 滑块最大距离
		private var _source:Object = null;	// 事件来源
		
		private var _isSeek:Boolean = false;

		public function KyuuProgressBar()
		{
			super();

			// 设置缺省宽高
			measuredMinWidth = 10;
			measuredMinHeight = 10;
			measuredWidth = negiBmp.width;
			measuredHeight = negiBmp.height;

			// 存储进度条最大宽度
			_proggressMax = negiBmp.width;

			// 构造影子位图
			var bmpData:BitmapData = new BitmapData(negiBmp.width, negiBmp.height, true);
			for(var x:int=0;x<negiBmp.width;x++){
				for(var y:int=0;y<negiBmp.height;y++){
					// 获取颜色值
					var color:uint = negiBmp.bitmapData.getPixel32(x ,y);
					// 获取透明通道
					var alphaValue:uint = (color >> 24) & 0xff;
					// 重新构造颜色
					color = (alphaValue << 24) | 0x666666;
					// 重设颜色
					bmpData.setPixel32(x, y, color);
				}
			}

			// 将影子位图绘制在背景上
			this.graphics.beginBitmapFill(bmpData);
			this.graphics.drawRect(0, 0, bmpData.width, bmpData.height);
			this.graphics.endFill();
		}

		// 重载 计算大小的方法
		override protected function measure():void{
			super.measure();

			this.width = negiBmp.width;
			this.height = negiBmp.height;
		}

		// 重载 构建子组件的方法
		override protected function createChildren():void{
			super.createChildren();

			// 添加进度条组件
			barImg = new Image();
			barImg.source = negiBmp;
			barImg.scaleContent = false;
			barImg.width = 0;
			barImg.height = negiBmp.height;
			this.addChild(barImg);
			barImg.x = 0;
			barImg.y = 0;
		}

		// 重载 初始换方法
		override public function initialize():void{
			super.initialize();

			this.addEventListener(ResizeEvent.RESIZE, resizeHandler);
		}

		// 捕获缩放事件
		private function resizeHandler(e:ResizeEvent):void{
			// 计算横向和纵向的缩放比例
			var sx:Number = this.width / negiBmp.width;
			var sy:Number = this.height / negiBmp.height;
			// 生成缩放矩阵
			var mtx:Matrix = new Matrix();
			mtx.scale(sx, sy);
			
			// 保存原始位置
			var backX:uint = this.x;
			var backY:uint = this.y;
			// 缩放组件
			this.transform.matrix = mtx;
			// 重置位置
			this.x = backX;
			this.y = backY;
			
			// 缩放进度条
			barImg.content.transform.matrix = mtx;
		}

		// setter 事件来源组件
		public function set source(value:Object):void{
			//trace("set source: " + value.toString());
			if(value is String){
				try{
					var sourceId:String = String(value);
					value = document[sourceId];
				}
				catch(e:Error){
					trace(e.message);
					value = null;
				}
			}
			if(value){
				if(value is EventDispatcher){
					_source = value;
					_source.addEventListener(ProgressEvent.PROGRESS, progressHandler);
					_source.addEventListener(Event.COMPLETE, completeHandler);
				}
				else
					trace("source is not EventDispatcher!");
			}
			else if(_source!=null){
				_source.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				_source.removeEventListener(Event.COMPLETE, completeHandler);
				_source = null;
			}
		}

		// method 设置进度条的状态
		public function setProgress(value:Number, total:Number):void{
			var barWidth:Number = _proggressMax * value / total;
			barImg.width = barWidth;

			// 调度进度变更事件
			var pe:ProgressEvent = new ProgressEvent(ProgressEvent.PROGRESS, false, false, value, total);
			dispatchEvent(pe);
		}

		// 进度进行中事件
		private function progressHandler(e:ProgressEvent):void{
			setProgress(e.bytesLoaded, e.bytesTotal);
		}

		// 进度完成事件
		private function completeHandler(e:Event):void{
			dispatchEvent(e);
		}
	}
}